<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jump</title>
<script type="text/javascript" src="../../lib/Box2dWeb-2.1.a.3.min.js"></script> 
<script type="text/javascript" src="../../lufylegend-1.8.12.simple.min.js"></script>
<script type="text/javascript" src="../../lib/lufylegend.LBox2d-0.4.0.min.js"></script>
</head>
<body style="margin:0px 0px 0px 0px;">
<div id="mylegend">loading……</div>
<script type="text/javascript">
init(10,"mylegend",800,480,main);
var backLayer,stageLayer;
var linelist;
var imglist;
var imgData = [
{name:"chara03",path:"3.png"},
{name:"chara04",path:"4.png"}];
var monseIsDown = false;
var hero;
var checkIndex = 0;
var mouseObject = {x:0,y:0};
var out = false;
var joinline;
var point = 0,name;
function main(){
	
	LLoadManage.load(
		imgData,
		function(progress){
		},
		function(result){
			imglist = result;
			gameInit();
		}
	);
}
function gameInit(){
	//LGlobal.setDebug(true); 
    stageLayer = new LSprite();  
    addChild(stageLayer);   
    backLayer = new LSprite();  
    addChild(backLayer);   
	
	labelText = new LTextField();
	labelText.color = "#000000";
	labelText.font = "宋体";
	labelText.size = 30;
	labelText.x = 80;
	labelText.y = 80;
	labelText.weight = "bolder";
	labelText.text = "box2d绳子示例";
	backLayer.addChild(labelText);
	
	labelText = new LTextField();
	labelText.color = "#000000";
	labelText.font = "宋体";
	labelText.size = 13;
	labelText.x = 100;
	labelText.y = 150;
	labelText.weight = "bolder";
	labelText.text = "请先输入您的昵称再开始游戏";
	backLayer.addChild(labelText);
	var labelLayer = new LSprite();
	labelLayer.graphics.drawRect(1,"#000000",[0,0,150,20],true,"#FFFFFF");
	nameText = new LTextField();
	nameText.x = 110;
	nameText.y = 180;
	nameText.setType(LTextFieldType.INPUT,labelLayer);
	backLayer.addChild(nameText);
	
	if(window.localStorage){
		if(window.localStorage.getItem("box2d_jump_name"))
		nameText.text=window.localStorage.getItem("box2d_jump_name");
	}
	var btn_up = new LSprite();
	labelText = new LTextField();
	labelText.color = "#000";
	labelText.font = "宋体";
	labelText.size = 16;
	labelText.x = 45;
	labelText.y = 8;
	labelText.text = "Game Start";
	btn_up.addChild(labelText);
	btn_up.graphics.drawRect(4,"#006400",[0,0,200,40]);
	var btn_down = new LSprite();
	labelText = new LTextField();
	labelText.color = "#000";
	labelText.font = "宋体";
	labelText.size = 16;
	labelText.x = 45;
	labelText.y = 8;
	labelText.text = "Game Start";
	btn_down.addChild(labelText);
	btn_down.graphics.drawRect(4,"#00FF00",[0,0,200,40]);
	btnReturn = new LButton(btn_up,btn_down);
	backLayer.addChild(btnReturn);
	btnReturn.x = 100; 
	btnReturn.y = 250;
	btnReturn.addEventListener(LMouseEvent.MOUSE_UP,function(event){
		if(nameText.text == ""){
			alert("请先输入姓名");
			return;
		}
		name = nameText.text;
		name = name.replace(/<*/g,'');
		name = name.replace(/>*/g,'');
		if(window.localStorage)window.localStorage.setItem("box2d_jump_name", name);
		setTimeout(gameStart,100);
	});
}
function gameStart(){
	backLayer.removeAllChild();
	backLayer.die();
    LGlobal.box2d = new LBox2d();
    
    wallLayer = new LSprite();
	wallLayer.y = 450;
	backLayer.addChild(wallLayer);
	wallLayer.addBodyPolygon(5000000,10,0);
	stageLayer.graphics.drawRect(1,"#ffffff",[0, 450, 5000, 10],true,"#000000");
    var bx = 250,by=40;
    var box01,box02;
	box01 = new LSprite();
	box01.x = bx;
	box01.y = 30;
	backLayer.addChild(box01);
	box01.addBodyCircle(10,0,0,0,1,10,0.2);
	box01.setBodyMouseJoint(true);
	linelist = [box01];
    for(var i=0;i<20;i++){
	    box02 = new LSprite();
	    box02.x = bx;
	    box02.y = by+i*10;
	    backLayer.addChild(box02);
	    box02.addBodyCircle(10,0,0,1,1,10,0.2);
	    LGlobal.box2d.setRevoluteJoint(box02.box2dBody, box01.box2dBody );
	    linelist.push(box02);
    	box01 = box02;
    }
    
	hero = new LSprite();
	var bit = new LBitmap(new LBitmapData(imglist["chara03"]));
	bit.x = -25;
	bit.y = -20;
	hero.addChild(bit);
	hero.bitmap = bit;
	hero.x = bx;
	hero.y = by+i*10;
	backLayer.addChild(hero);
	hero.addBodyPolygon(30,50,1,2,10,.2);
	joinline = LGlobal.box2d.setRevoluteJoint(hero.box2dBody, box01.box2dBody );
	
	backLayer.addEventListener(LEvent.ENTER_FRAME,onframe);
	backLayer.addEventListener(LMouseEvent.MOUSE_DOWN,ondown);
	backLayer.addEventListener(LMouseEvent.MOUSE_UP,onup);
	
}
function onframe(){
	backLayer.graphics.clear();
	backLayer.graphics.drawRect(1,"#000000",[0,0,LGlobal.width,LGlobal.height]);
	for(var i=0;i<linelist.length - 1;i++){
		backLayer.graphics.drawLine(2,"#000000",[linelist[i].x,linelist[i].y,linelist[i+1].x,linelist[i+1].y]);
	}
	
	if(monseIsDown && !out){	
		if(checkIndex++ > 10){
			checkIndex = 0;
			if(LGlobal.offsetX - mouseObject.x > 50){
				var force = 50;
				var vec = new LGlobal.box2d.b2Vec2(force,0);
				hero.box2dBody.ApplyForce(vec, hero.box2dBody.GetWorldCenter());
			}else if(LGlobal.offsetX - mouseObject.x < -50){
				var force = 50;
				var vec = new LGlobal.box2d.b2Vec2(-force,0);
				hero.box2dBody.ApplyForce(vec, hero.box2dBody.GetWorldCenter());
			}
			mouseObject.x = LGlobal.offsetX;
		}	
	}	
	if(!out)return;
	backLayer.x = LGlobal.width*0.5 - hero.x;
	if(backLayer.x > 0){
		backLayer.x=0;
	}
	LGlobal.box2d.synchronous();
	if(!hero.box2dBody.IsAwake() && out){
		backLayer.removeEventListener(LEvent.ENTER_FRAME,onframe);
		point = Math.floor((hero.x - 250)*0.1);

		var rank = new GameRanking();
		stageLayer.addChild(rank);
	}
}
function onup(event){	
	if(out)return;
	monseIsDown = false;
	LGlobal.box2d.world.DestroyJoint(joinline);
	hero.bitmap.bitmapData = new LBitmapData(imglist["chara04"])
	hero.bitmap.x = 0;
	hero.bitmap.y = 0;
	out = true;
}
function ondown(event){	
	if(out)return;
	monseIsDown = true;	
	mouseObject.x = event.offsetX;
}
function GameRanking(){
	base(this,LSprite,[]);
	var self = this;
	//name = "";
	self.graphics.drawRect(4,"#00000",[200,70,400,100],true,"#FFFFFF");
	labelText = new LTextField();
	labelText.color = "#006400";
	labelText.size = 20;
	labelText.weight = "bolder";
	labelText.text = name+"  距离:"+point+"m";
	labelText.x = 220;
	labelText.y = 80;
	self.addChild(labelText);
	
	labelText = new LTextField();
	labelText.color = "#FF0000";
	labelText.size = 20;
	labelText.weight = "bolder";
	labelText.text = "成绩上传中...";
	labelText.x = 250;
	labelText.y = 120;
	self.addChild(labelText);
	//成绩上传略
	setTimeout(function(){
		location.href = "ranking.html";
	}, 1000 );
};
</script> 
</body>
</html>